package com.qidian.study.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 */
public class RedisUtil {
    private static String host = "192.168.66.132";
    private static String port = "6379";
    private static JedisPool pool;
    static {
        Properties ps = new Properties();
        try {
            ps.load(RedisUtil.class.getClassLoader().getResourceAsStream("redis.properties"));
            host = ps.getProperty("redis.host");
            port = ps.getProperty("redis.port");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public synchronized static Jedis getJedis(){
        return new Jedis(host,new Integer(port));
    }

    public static String set(String key,String value){
        Jedis jedis = getJedis();
        String result = jedis.set(key,value);
        jedis.close();;
        return result;
    };

    public static String get(String key){
        Jedis jedis = getJedis();
        String result = getJedis().get(key);
        jedis.close();
        return result;
    }

    public static Long hset(String key,String field,String value){
        Jedis jedis = getJedis();
        Long result = getJedis().hset(key,field,value);
        jedis.close();
        return result;
    }
    public static String hmset(String key, Map map){
        Jedis jedis = getJedis();
        String result = getJedis().hmset(key,map);
        jedis.close();
        return result;
    }
    public static String hget(String key,String filed){
        Jedis jedis = getJedis();
        String result = getJedis().hget(key,filed);
        jedis.close();
        return result;
    }
    public static Map hgetAll(String key){
        Jedis jedis = getJedis();
        Map result = getJedis().hgetAll(key);
        jedis.close();
        return result;
    }

    public static Long expire(String key,int second){
        Jedis jedis = getJedis();
        Long result = getJedis().expire(key,second);
        jedis.close();
        return result;
    }

    public static Long persist(String key){
        Jedis jedis = getJedis();
        Long result = getJedis().persist(key);
        jedis.close();
        return result;
    }

    public static Long del(String key){
        Jedis jedis = getJedis();
        Long result =  getJedis().del(key);
        jedis.close();
        return result;
    };

    public static boolean exists(String key){
        Jedis jedis = getJedis();
        boolean result = getJedis().exists(key);
        jedis.close();
        return result;
    }
}
